﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Fc
{
    public partial class 保护器出库单录入 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;

            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }

                if (id > 0)
                {
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = id.ToString();
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    DateTime d = DateTime.Today;

                    this.Date1.Text = d.ToLongDateString();
                }
            }
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {

            int taoshu;
            if (!int.TryParse(this.TaoShu.Text, out taoshu) || taoshu <= 0)
            {
                this.Notice = "“套数”录出有误，请录出一个正整数";
                return;
            }
            int jianshu;
            if (!int.TryParse(this.JianShu.Text, out jianshu) || jianshu <= 0)
            {
                this.Notice = "“件数”录出有误，请录出一个正整数";
                return;
            }

            DateTime d1;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                this.Notice = "“日期”录出有误";
                return;
            }
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }

            //开始保存数据
            if (this.BillID.Text == "0")
            {
                ParameterCollection pc = this.SqlDataSource1.InsertParameters;
                pc["制票"].DefaultValue = op;

                if (this.SqlDataSource1.Insert() > 0)
                {
                    this.Notice = "保存成功";
                }
                else
                {
                    this.Notice = "保存失败";
                }
            }
            else
            {
                ParameterCollection pc = this.SqlDataSource1.UpdateParameters;
                pc["制票"].DefaultValue = op;
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Notice = "保存成功";
                }
                else
                {
                    this.Notice = "保存失败";
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
                this.Date1.Text = ((DateTime)e.Command.Parameters[1].Value).ToLongDateString();
                this.FenChang.Text = ((int)e.Command.Parameters[2].Value).ToString();
                this.TaoShu.Text = (-(int)e.Command.Parameters[3].Value).ToString();
                this.JianShu.Text = (-(int)e.Command.Parameters[4].Value).ToString();

                if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
                {
                    this.BeiZhu.Text = (String)e.Command.Parameters[5].Value;
                }
            }
        }
    }
}
